package n.d.l.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import n.d.l.svn.bean.SysCommandResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Utils {

	private static final Logger log = LoggerFactory.getLogger(Utils.class);
	
	/**
	 * 执行命令
	 * @param command 命令
	 * @param file 执行命令文件夹
	 * @return
	 */
	public static List<String> doCommand(String command, File file) {
		Process process = null;
		try {
			process = Runtime.getRuntime().exec(command, null, file);
			InputStream in = process.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in, "gbk"));
			String line = null;
			List<String> lines = new ArrayList<String>(); 
			while ((line = reader.readLine()) != null) {
				lines.add(line);
				log.debug(line);
			}
			//process.waitFor();
			return lines;
		} catch (IOException e) {
			e.printStackTrace();
		/*} catch (InterruptedException e) {
			e.printStackTrace();
		*/} finally {
			if (process != null) {
				process.destroy();
			}
		}
		return null;
	}
	
	/**
	 * 执行命令
	 * @param command 命令
	 * @param file 执行命令文件夹
	 * @return
	 */
	public static SysCommandResponse getCommandResponse(String command, File file) {
		SysCommandResponse commandResp = new SysCommandResponse();
		try {
			Process proces = Runtime.getRuntime().exec(command, null, file);
			InputStream in = proces.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in, "gbk"));
			commandResp.setReader(reader);
			commandResp.setProces(proces);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return commandResp;
	}
	
	public static void listPrintln(List<String> list){
		for(String o : list){
			System.out.println(o);
		}
	}
	
	public static void main_bak(String[] args) {
		doCommand("svn log", new File("F:/Workspaces/eclipse/oracle/XDW_GOM_API"));
	}

}
